//
//  FileWriteManager.swift
//  Trans
//
//  Created by zcj on 2025/2/11.
//

import Foundation

class FileWriteManager{
    static let shared: FileWriteManager = FileWriteManager()
    var fileDic:Dictionary = Dictionary<FileId, FileDataMsg>()
    
    
    func addObjForKey(key:FileId,fdm:FileDataMsg){
        fileDic[key] = fdm
    }
    
    
    func writeData(fileId:FileId,data:Data){
        if let fdm = fileDic[fileId]{
            fdm.addData(data: data)
        }else{
            warn_log("writeData not file id : \(fileId)")
        }
    }
    
    func flushData(fileId:FileId){
        if let fdm = fileDic[fileId]{
            fdm.writeToFile()
        }else{
            warn_log("flushData not file id : \(fileId)")
        }
    }
    
    func flushData(fileId:FileId,bIsEnd:Bool = false){
        if let fdm = fileDic[fileId]{
            fdm.writeToFile()
        }else{
            warn_log("flushData not file id : \(fileId)")
        }
        if bIsEnd{
            fileDic.removeValue(forKey: fileId)
        }
    }
    
    func removeObjForKey(fileId:FileId){
        if fileDic.keys.contains(fileId){
            fileDic.removeValue(forKey: fileId)
        }
    }
}
